﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ProjBackEnd.Areas.BizProj.Models;
using ProjBackEnd.Lib;
using ProjBackEnd.DAL;

namespace ProjBackEnd.Areas.BizProj.Controllers
{
    /// <summary>
    /// 业务费用操作
    /// </summary>
    public class BizFeeOptController : ApiController
    {
        /// <summary>
        /// 获取业务费用明细
        /// eg.：0000000001|COM1|WEB|1|0|0|R|0|0|1
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|项目id（0=所有）|项目明细id（0=所有；-1=无）|账单id（0=所有）|费用pid（空值=所有）|收付标志（R-收；P-付；空值=所有）|客户编号（0=所有）|合同id（0=所有）|合同子项id（0=所有）|建档人（空值=所有）</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizproj/bizfeeopt")]
        public RespBizFee GetBizFee(string key)
        {
            RespBizFee resp = new RespBizFee();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 12)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            string strPid = strsKeyOriginal[6];
            string strRplb = strsKeyOriginal[7].ToUpper();
            string strCreatid = strsKeyOriginal[11];
            int iProjid, iProjdtlid, iBillid, iCusid,iCtid,iCtSubid;
            if (!int.TryParse(strsKeyOriginal[3], out iProjid)
                || !int.TryParse(strsKeyOriginal[4], out iProjdtlid)
                || !int.TryParse(strsKeyOriginal[5], out iBillid)
                || !int.TryParse(strsKeyOriginal[8], out iCusid)
                || !int.TryParse(strsKeyOriginal[9], out iCtid)
                || !int.TryParse(strsKeyOriginal[10], out iCtSubid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            if (iProjid == 0 && iProjdtlid == 0 && iBillid == 0 && iCusid == 0)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "所有索引编号不能同时为 0";
                return resp;
            }

            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.GetBizFeeList(strMemberid, strComcd, iProjid, iProjdtlid, iBillid, strPid, strRplb, iCusid, iCtid, iCtSubid, strCreatid);
            return resp;
        }

        /// <summary>
        /// 获取费用归属信息
        /// </summary>
        /// <param name="key">用户id|公司代码|平台代码|费用id</param>
        /// <returns></returns>
        [HttpGet]
        [Route("api/bizproj/bizfeeheader")]
        public RespBizFeeHeader GetBizFeeHeader(string key)
        {
            RespBizFeeHeader resp = new RespBizFeeHeader();
            if (string.IsNullOrEmpty(key))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            string[] strsKeyOriginal = DataUtil.PlaintextConfusion(key, DataUtil.enumDeEncryptType.Decrypt).Split('|');
            if (strsKeyOriginal.Length < 4)
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            string strMemberid = strsKeyOriginal[0];
            string strComcd = strsKeyOriginal[1];
            string strPlatcd = strsKeyOriginal[2];
            int iFeeid;
            if (!int.TryParse(strsKeyOriginal[3], out iFeeid))
            {
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误";
                return resp;
            }
            if (!userLogic.CheckToken(strMemberid, strToken, strPlatcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.GetBizFeeHeaderInfo(iFeeid);
            return resp;
        }

        /// <summary>
        /// 维护业务费用基本信息
        /// </summary>
        /// <param name="_reqInfo">更新请求</param>
        /// <returns></returns>
        [HttpPost]
        [Route("api/bizproj/bizfeeopt")]
        public RespProjNullResultInfo UpdateBizFeeBaseInfo(ReqBizFeeBase _reqInfo)
        {
            RespProjNullResultInfo resp = new RespProjNullResultInfo();
            if (!ModelState.IsValid)
            {
                string strErrmsg = DataUtil.GetModelStateError(ModelState);
                resp.statusCode = 90004;
                resp.statusMsg = "请求数据格式错误。" + strErrmsg;
                return resp;
            }

            string strToken = DataUtil.CheckTokenValid(Request);
            if (strToken == "")
            {
                resp.statusCode = 90002;
                resp.statusMsg = "非法请求";
                return resp;
            }

            if (!userLogic.CheckToken(_reqInfo.memberid, strToken, _reqInfo.platcd))
            {
                resp.statusCode = 90001;
                resp.statusMsg = "用户令牌无效";
                return resp;
            }

            resp = projLogic.UpdateBizFeeBaseInfo(_reqInfo.memberid, _reqInfo.comcd, _reqInfo.bizfeeinfo);
            return resp;
        }

    }
}
